#define  _CRT_SECURE_NO_WARNINGS 1

#include "game.h"

//初始化棋盘-->函数定义
void InitBoard(char board[ROWS][COLS], int rows, int cols, char set)
{ 
	int i, j = 0;

    if (set == '0')
    {
        for (i = 0; i < rows; i++)
        {
            for (j = 0; j < cols; j++)
            {
                board[i][j] = '0';
            }
        }

    }
    else if (set == '*')
    { 
        for (i = 0; i < rows; i++)
        {
            for (j = 0; j < cols; j++)
            {
                board[i][j] = '*';
            }
        }
    }
}

//打印棋盘
void DisplayBoard(char board[ROWS][COLS], int row, int col)
{
    int i, j = 0;
    printf("----------扫雷游戏----------\n");

    for (i = 0; i <= col; i++)//输出列号
    {
        printf(" %d ", i);
    }
    printf("\n");
    for (i = 1; i <= row; i++)
    {
        printf(" %d ", i);//输出行号
        for (j = 1; j <= col; j++)
        {
            printf(" %c ", board[i][j]);
        }
        printf("\n");
    }
    printf("\n");
}

//布置雷
void SetMine(char board[ROWS][COLS], int row, int col)
{
    //随机布置10个雷
    int count = EASY_TEST;
    //循环次数>=10
    while (count)//通过限制循环次数来限制随机数个数
    {
        //随机生成坐标1~9
        int x = rand() % row + 1;//1~9
        int y = rand() % col + 1;

        if (board[x][y] == '0')//这个位置不是雷
        {
            board[x][y] = '1';//布置雷
            count--;
        }

    }

}

static int get_mine_around(char mine[ROWS][COLS], int x, int y) //仅在本文件中使用，只支持下面的调用函数
{
    //方法一：（麻烦）
    // 
    //int count = 0;
    //if (board[x - 1][y - 1] == '1')
    //{
    //    count++;
    //}
    //if (board[x - 1][y] == '1')
    //{
    //    count++;
    //}
    //if (board[x - 1][y + 1] == '1')
    //{
    //    count++;
    //}
    //if (board[x][y - 1] == '1')
    //{
    //    count++;
    //}
    //if (board[x][y + 1] == '1')
    //{
    //    count++;
    //}
    //if (board[x + 1][y - 1] == '1')
    //{
    //    count++;
    //}
    //if (board[x + 1][y] == '1')
    //{
    //    count++;
    //}
    //if (board[x + 1][y + 1] == '1')
    //{
    //    count++;
    //}
    //return count;

    //方法二：将周围的个数相加
    return mine[x - 1][y - 1] + mine[x - 1][y] + mine[x - 1][y + 1] + mine[x][y - 1] + mine[x][y + 1] + mine[x + 1][y - 1] + mine[x + 1][y] + mine[x + 1][y + 1] - 8 * '0';
}

//排查雷
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
    int x, y = 0;
    int win = 0;
    while (win<row*col - EASY_TEST)
    {
        //DisplayBoard(mine, ROW, COL);//验证坐标、数据是否正确
        printf("请输入要排查的坐标：\n");
        scanf("%d%d", &x, &y);

        if (x >= 1 && x <= row && y >= 1 && y <= col)
        {
            if (mine[x][y] == '1')
            {
                printf("很遗憾，你被炸死了\n");
                DisplayBoard(mine, ROW, COL);
                break;
            }
            else
            {
                //win++;  error：如果重复排查某坐标，win++会重复加1
                if (show[x][y] != '*')
                {
                    printf("该坐标已排查过，请勿重复排查\n");
                    system("pause");
                    system("cls");
                    DisplayBoard(show, ROW, COL);
                }
                else
                {
                    win++;
                    //1.统计周围有几个雷
                    int count = get_mine_around(mine, x, y);
                    //2.存到show数组中
                    show[x][y] = count + '0';//转换为数字字符

                    system("cls");
                    DisplayBoard(show, ROW, COL);
                }
                
                 
            }
        }

        else
        {
            printf("坐标输入错误，请重新输入：");
        }
    }
    if (win == row*col - EASY_TEST)
    {
        printf("恭喜你，排雷成功\n");
        DisplayBoard(mine, ROW, COL);
    }

}

